package quest.uiproxy
{
import core.manager.UIAssetsManager;

import flash.display.DisplayObject;
import flash.events.Event;

import framework.core.ComponentBuilder;

import quest.QuestManager;

import utils.queen.Queen;
import utils.queen.QueenList;

import values.quest.QuestEventValue;
import values.quest.QuestSettingValue;
import values.quest.QuestValue;

import view.interfaces.ILeftUI;
import view.quest.IQuestComponent;
import view.quest.QuestIconRender;

/**
 * QuestUIProxy
 * @author jliang
 * 
 */
public class QuestUIProxy implements IQuestUIProxy
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function QuestUIProxy()
	{
		ui = Game.instance.leftUI;
	}
	//==========================================================================
	//  Variables
	//==========================================================================
	private var ui:ILeftUI;
	
	private var uiQueen:QueenList;
	//==========================================================================
	//  Public methods
	//==========================================================================
	public function questAppear(questValue:QuestValue):void
	{
		ui.addElement(questValue.template.key, 
							  createQuestIconRender(questValue),
							  questValue,
							  clickQuestIconRender);
		
		createUIByEvent(questValue, "appear");
	}
	
	public function questAccept(questValue:QuestValue):void
	{
		createUIByEvent(questValue, "accept");
	}
	
	public function viewQuestDetail(questValue:QuestValue):void
	{		
		var event:QuestEventValue = questValue.template.getEventByType("view_detail");
		if (event && event.ui.length > 0)
		{
			createUIByEvent(questValue, "view_detail");
			return;
		}
		
		var detailUI:IQuestComponent = IQuestComponent(UIAssetsManager.createAssetByDef(QuestSettingValue.getUIDefByKey("$DETAIL")));
		detailUI.data = questValue;
		detailUI.show();
	}
	
	public function questProgress(questValue:QuestValue):void
	{
		createUIByEvent(questValue, "progress");
	}
	
	public function questComplete(questValue:QuestValue):void
	{
		ui.removeElement(questValue.template.key);
		
		var event:QuestEventValue = questValue.template.getEventByType("complete");
		if (event && event.ui.length > 0)
		{
			createUIByEvent(questValue, "complete");
			return;
		}
		
		var completeUI:IQuestComponent = IQuestComponent(UIAssetsManager.createAssetByDef(QuestSettingValue.getUIDefByKey("$COMPLETE")));
		completeUI.data = questValue;
		completeUI.show();
	}
	
	private function createQuestIconRender(questValue:QuestValue):DisplayObject
	{
		var render:QuestIconRender = ComponentBuilder.create(QuestIconRender);
		render.data = questValue;		
		return render;
	}
	
	private function clickQuestIconRender(questValue:QuestValue):void
	{
		if (questValue.isAccepted)
		{
			QuestManager.instance.viewQuestDetail(questValue);
		}
		else
		{
			QuestManager.instance.questAccept(questValue);
			
			var event:QuestEventValue = questValue.template.getEventByType("accept");
			if (!event || event.ui.length == 0)
			{
				QuestManager.instance.viewQuestDetail(questValue);
			}
		}
	}
	
	private function createUIByEvent(questValue:QuestValue, eventType:String):void
	{
		var event:QuestEventValue = questValue.template.getEventByType(eventType);
		
		queenComplete(null);
		
		if (!event || event.ui.length == 0) return;
		
		uiQueen = new QueenList();		
		uiQueen.addEventListener(Event.COMPLETE, queenComplete);
		
		for (var i:int = 0; i < event.ui.length; i++)
		{
			var asset:IQuestComponent;
			var uiKey:String = event.ui[i];
			var uiDef:*;
			if (uiKey.indexOf("$") == 0)
			{
				uiDef = QuestSettingValue.getUIDefByKey(uiKey);
			}
			else
			{
				uiDef = questValue.template.getUIAssetDefByKey(uiKey)
			}
			asset = IQuestComponent(UIAssetsManager.createAssetByDef(uiDef));
			asset.data = questValue;
			
			var queen:Queen = new Queen();
			queen.handler = new QuestUIQueenHandler();
			
			var qd:QuestUIQueenData = new QuestUIQueenData();
			qd.vo = questValue;
			qd.ui = asset;
			queen.data = qd;
			
			uiQueen.addQueen(queen);
		}
		
		uiQueen.start();
	}
	
	private function queenComplete(event:Event):void
	{
		if (uiQueen)
		{
			uiQueen.removeEventListener(Event.COMPLETE, queenComplete);
			uiQueen.cancel();
			uiQueen = null;
		}
	}
	
}
}



